
var rp = require('request-promise')

exports.main = async (event, context) => {
  var _token =  await rp(`https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appId}&secret=${appSecret}`).then( res => {
   return JSON.parse(res).access_token
  }).catch( err => {
    console.log(err);
  })

  console.log(_token)
  if(!_token || _token == undefined){
      return{
          err:'token is null'
      }
  }
  var options= {
    method:'post',
    url:"https://api.weixin.qq.com/wxa/getwxacode?access_token="+_token,
   
    headers:{
        'User-Agent': 'Request-Promise'
    },
    body: JSON.stringify({
        path: event.path
    }),
    encoding: null
    };
    //返回二维码图片绘制
    let qrcode = rp(options).then(function(data){
        console.log(data)
        resolve(res.body)
    });
    return{
        qrcode:qrcode
    }
   
}